#include <REGX52.H>
#include "Delay.h"
#include "LCD1602.h"
#include "MatrixKey.h"

unsigned char KeyNum = 0;
unsigned int Password = 0;
int count = 0;
void main(){
	LCD_Init();
	LCD_ShowString(1, 1, "Password:");
	while(1){
		KeyNum = KeyNumber();
		if(KeyNum){
			if(KeyNum <= 10){	//输入1-10进行判断
				if(count<4){	//输入次数不超过四位，密码不超过四位
					Password *= 10; // 密码移位
					Password += KeyNum%10;
					count++;	//计数器++
					LCD_ShowNum(2, 1, Password, 4);//判断后更新显示
				}
			}
			if(KeyNum == 12){	//取消输入，回退输入
				Password = Password / 10;
				count--;
				LCD_ShowNum(2, 1, Password, 4);//判断后更新显示
			}
			if(KeyNum == 13){	//清空密码
				Password = 0;
				count = 0;
				//LCD_ShowNum(2, 1, Password, 4);//判断后更新显示
				LCD_ShowString(2, 1, "                ");
			}
			
			if(KeyNum == 11){	//如果按下11 代表确认
				if(Password == 1225){	//正确的话相应的显示
					LCD_ShowString(2, 1, "Happy Birthday");
				}
				else{
					LCD_ShowString(2, 1, "Error");
					Password = 0;
					count = 0;
					//LCD_ShowNum(2, 1, Password, 4);//判断后更新显示
				}
					
			}
			
		}
	}
}